#ifndef LGE_MEMORY_H
#define LGE_MEMORY_H

#include <core/lge_types.h>
#include <malloc.h>
#include <string.h>

/**
  * \brief Allocate memory
  *
  * \param elemCount The number of elements of sizeof(T) to allocate
  * \return NULL if memory allocation failed, or a valid address if memory
  *         allocation succeed
  *
  * \pre elemCount should not be null
  */
template<typename T>
inline T* lgeAlloc(SizeT elemCount)
{
  return (T *)_aligned_malloc(sizeof(T) * elemCount, 16);
}

/**
  * \brief Free memory and set the pointer to NULL
  *
  * \param[in,out] addr A pointer to memory previously allocated by lgeAlloc.
  *                This will be null'ed after the call.
  *
  * \pre addr should not be null and should have been allocated by lgeAlloc
  * \post addr is null and memory has been freed
  */
inline void lgeFree(void * & addr)
{
	_aligned_free(addr);
	addr = NULL;
}

/**
  * \brief Perform a memory copy of elemCount x sizeof(T) bytes from src to 
  *        dest
  *
  * \param dest The destination where the data will be copied to
  * \param src The source from where the data will be copied from
  * \param elemCount The number of elements of size sizeof(T) which will be
  *        copied to dest address
  */
template<typename T>
inline void lgeMemCopy(T * dest, const T * src, SizeT elemCount=1)
{
  memcpy(dest, src, sizeof(T) * size);
}

/**
  * \brief Zero fill a memory zone
  */
template<typename T>
inline void lgeMemZero(T * dest, SizeT elemCount=1)
{
  memset(dest, 0x00, sizeof(T) * elemCount);
}

#endif
